from datetime import datetime
import hashlib
from flask import json
from exts import db
from sqlalchemy.dialects.mysql import JSON
from werkzeug.security import generate_password_hash, check_password_hash
from sqlalchemy import func
from models.warehouse import (
    Item,
    Inventory,
    Warehouse,
    AuditRecord,
    AuditStatus,
    InventoryStatus,
)
from services.user_service import *
from services.warehouse_service import *
from services.item_service import *
from services.inventory_service import *


class item_service:

    def get_item_by_id(self, id):
        if not id:
            raise Exception("物品ID不能为空")
        item = Item.query.filter(Item.id == id).first()
        if not item:
            raise Exception("物品不存在")
        return {
            "id": item.id,
            "name": item.name,
            "description": item.description,
            "created_by": item.created_by,
            "created_name": user_service().get_user_info(item.created_by)["username"],
        }

    def get_item_all(self):
        items = Item.query.all()
        if items is None:
            return []
        return [
            {
                "id": item.id,
                "name": item.name,
                "description": item.description,
                "created_by": item.created_by,
                "created_name": user_service().get_user_info(item.created_by)[
                    "username"
                ],
            }
            for item in items
        ]

    def exist_item(self, name):
        if not name:
            raise Exception("物品名称不能为空")
        item = Item.query.filter(Item.name == name).first()
        return item

    def create_item(self, post_json, created_by):
        # 使用 get 方法安全地获取字典中的值
        if not post_json.get("name") or not post_json.get("description"):
            raise Exception("物品名称和品类不能为空")

        name = post_json.get("name")
        description = post_json.get("description")
        created_by = created_by

        if self.exist_item(name):
            raise Exception("物品已存在")
        new_item = Item(name=name, description=description, created_by=created_by)
        db.session.add(new_item)
        db.session.commit()
        db.session.refresh(new_item)
        return self.get_item_by_id(new_item.id)

    def update_item(self, put_json):
        if put_json:
            if not put_json.get("id"):
                raise Exception("物品ID不能为空")
            item_id = put_json.get("id")
            # 直接查询 Item 实例
            item = Item.query.filter(Item.id == item_id).first()
            if not item:
                raise Exception("物品不存在")
            if put_json.get("name"):
                item.name = put_json.get("name")
            if put_json.get("description"):
                item.description = put_json.get("description")
            if put_json.get("created_by"):
                item.created_by = put_json.get("created_by")
            db.session.commit()
            db.session.refresh(item)
            return self.get_item_by_id(item.id)
        else:
            raise Exception("物品信息不能为空")

    def delete_item(self, delete_json):
        if delete_json:
            if not delete_json.get("id"):
                raise Exception("物品ID不能为空")
            item_id = delete_json.get("id")
            item = Item.query.get(item_id)
            if not item:
                raise Exception("物品不存在")
            # 在删除前获取物品信息
            item_info = {
                "id": item.id,
                "name": item.name,
                "description": item.description,
                "created_by": item.created_by,
                "created_name": user_service().get_user_info(item.created_by)["username"],
            }
            db.session.delete(item)
            db.session.commit()
            return item_info
        else:
            raise Exception("物品信息不能为空")
